<?php
/**
 * Description of Utilities
 *
 * @author Ernesto
 */
class Utilities {
    private $objDAO;
    private static $instance = null;
    
    private function __construct() {
        require_once dirname(__FILE__).'/source/GenericDAO.class.php';
        $this->objDAO = GenericDAO::getInstance();
    }
    //Retorna el ID de la persona en caso de estar registrado de lo contrario retorna FALSE
    public function estaRegistradoUsuario($cedula){
	$result = $this->objDAO->select(array('table'=>'personas', 'fields' =>'id_persona', 'condition' => 'cedula="'.$cedula.'"'));
        try {
            if(isset($result[0]['personas']['id_persona'])){
                return $result[0]['personas']['id_persona'];
            }else{
                throw new Exception();
            }
        } catch (Exception $exc) {
            return false;
        }
    }
    
    public static function getInstance(){
      if (!self::$instance instanceof self){
         self::$instance = new Utilities;
      }
      return self::$instance;
    }
    
    public function consecutivo_tabla($tabla){
	$sql = "SHOW TABLE STATUS LIKE '$tabla'";
	$result = $this->objDAO->query($sql);
        
        try {
            if(isset($result[0]['TABLES']['Auto_increment'])){
                return $result[0]['TABLES']['Auto_increment'];
            }else{
                throw new Exception();
            }
        } catch (Exception $exc) {
            return '0';
        }
    }
    
    public function rol_de_usuario($id_usuario='null'){
        $result =  $this->objDAO->select(array('table'=> 'usuarios', 'fields' => 'id_rol', 'condition' => 'id_usuario="'.$id_usuario.'"'));
        try {
            if(isset($result[0]['usuarios']['id_rol'])){
                return $result[0]['usuarios']['id_rol'];
            }else{
                throw new Exception();
            }
        } catch (Exception $exc) {
            return '-1';
        }
    }
    
    public function limpiar_caracteres_especiales($s){
        $s = ereg_replace("[áàâãª]","a",$s);
        $s = ereg_replace("[ÁÀÂÃ]","A",$s);
        $s = ereg_replace("[éèê]","e",$s);
        $s = ereg_replace("[ÉÈÊ]","E",$s);
        $s = ereg_replace("[íìî]","i",$s);
        $s = ereg_replace("[ÍÌÎ]","I",$s);
        $s = ereg_replace("[óòôõº]","o",$s);
        $s = ereg_replace("[ÓÒÔÕ]","O",$s);
        $s = ereg_replace("[úùû]","u",$s);
        $s = ereg_replace("[ÚÙÛ]","U",$s);
        $s = ereg_replace("[ü]","u",$s);
        $s = ereg_replace("[Ü]","U",$s);
        $s = str_replace(" "," ",$s);
        $s = str_replace("ñ","n",$s);
        $s = str_replace("Ñ","N",$s);
        return $s;
    }
    
    public function limpiar_caracteres_especiales2($texto){ 
        $texto = utf8_decode($texto);
        $con_acento = utf8_decode("ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ");
        $sin_acento = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
        $texto = strtr($texto, $con_acento, $sin_acento);
        $texto = preg_replace("/[^A-Za-z0-9 _]/","",$texto);
        $texto = strtoupper(trim ($texto));
        return $texto;
    }
    
    public function puede_ver($id_usuario=null){
        $result = $this->objDAO->select(array('table' => 'usuarios', 'fields' => 'id_usuario', 'condition' => 'id_usuario="'.$id_usuario.'"'));
        
        try {
            if(isset($result[0]['usuarios']['id_usuario'])){
                return true;
            }else{
                throw new Exception();
            }
        } catch (Exception $exc) {
            return false;
        }
    }
    
    public function ciudades(){
	$result = $this->objDAO->select(array('table'=> 'ciudades', 'fields'=> 'id_ciudad, UPPER(nombre) as nombre', 'condition'=> 'ID_DPTO =076'));
        $ciudades='';
        foreach($result as $ciudad){
            $contentCiudad = sprintf('<option value="%s">%s</option>',$ciudad[0]['nombre'],$ciudad[0]['nombre']);
            $ciudades = $ciudades.$contentCiudad;
        }
        $contentCiudades = sprintf("<select class='span12' name='ciudad_r' id='ciudad' data-mini='true' class='required' data-native-menu='false'><option value='ZZ'>Selecciona una opci&oacute;n</option>%s</select>",$ciudades);
        return $contentCiudades;
    }
    
    public function departamentos(){
        $result = $this->objDAO->select(array('table'=> 'departamentos', 'fields'=> 'id_dpto, UPPER(nombre) as nombre'));
        $departamentos='';
        foreach($result as $departamento){
            $contentDepartamento = sprintf('<option value="%s">%s</option>',$departamento[0]['nombre'],$departamento[0]['nombre']);
            $departamentos = $departamentos.$contentDepartamento;
        }
        $contentDepartamentos = sprintf("<select name='departamento_r' id='departamento' data-mini='true' class='required' data-native-menu='false'><option value='ZZ'>Selecciona una opci&oacute;n</option>%s</select>",$departamentos);
        return $contentDepartamentos;
    }
}

?>
